import {Injectable} from '@angular/core';
import request from '../../../utils/request';

const base_url = 'builder-api/programme/';

@Injectable()
export class ProgrammeService {

  getProgrammeById = function (id) {
    return request({
      url: base_url + id,
      method: 'get',
      data: {id},
    });
  };
  saveProgramme = function (entity) {
    if (entity != null) {
      return request({
        url: base_url,
        method: 'post',
        data: entity,
      });
    } else {
      throw new Error('保存的信息不能为空');
    }
  };
  updateProgramme = function (entity) {
    if (entity != null) {
      if (entity.id != null) {
        return request({
          url: base_url + entity.id,
          method: 'put',
          data: entity,
        });
      } else {
        throw new Error('保存的信息id必须为空');
      }
    } else {
      throw new Error('保存的信息不能为空');
    }
  };
  updateProgrammeBatch = function (keys, entity) {
    let ids_ = '';
    if (keys instanceof Array) {
      ids_ = keys.join(',');
    } else if (typeof keys === 'string') {
      ids_ = keys;
    } else {
      throw new Error('不支持的主键类型');
    }
    if (entity != null) {
      return request({
        url: base_url + ids_,
        method: 'patch',
        data: entity,
      });
    } else {
      throw new Error('保存的信息不能为空');
    }
  };
  deleteProgramme = function (entity) {
    if (entity != null) {
      if (entity.id != null) {
        return request({
          url: base_url + entity.id,
          method: 'delete',
        });
      } else {
        throw new Error('删除的数据还没有保存');
      }
    } else {
      throw new Error('保存的信息不能为空');
    }
  };
  deleteProgrammes = function (ids) {
    if (ids != null && ids.length > 0) {
      return request({
        url: base_url + ids,
        method: 'delete',
      });
    } else {
      throw new Error('保存的信息不能为空');
    }
  };
  findProgrammeByList = function (params) {
    return request({
      url: base_url,
      method: 'get',
      data: params,
    });
  };

  findProgrammeByPage(params) {
    return request({
      url: base_url + 'findByPage',
      method: 'get',
      data: params,
    });
  }

}
